<?php

class add_shipping extends a_admin_controller {

    protected $form_data;
    protected $form_errors;
    protected $user_id;

    /*
     * @TODO: function get_groups_n_field() does not seem to
     * work when it is being called from index function, only from constructor.
     *
     * *edit: this currently may not need to change since the issue of receiving
     * a post has been handled. I am receiving the post from the function
     * get_groups_n_field()
     */
    public function __construct($user_id=0) {
        parent::__construct(true);
        $this->form_data = array();
        $this->get_groups_n_fields();
    }

    public function batch_assign(){
        $this->assign("form_data", $this->form_data);
        $this->assign("form_errors", $this->form_errors);
        $this->assign("user_id", $this->user_id);
    }

    public function index() {
    }

    public function add_shipping_address(){
        $this->user_id = $this->input->post('user_id');
        
        if($this->form->valid){
            $vars = $this->form->get_post();

            user_service_l_user::add_user_shipping_address($vars);

            redirect(base_url()."index.php/user/admin/user_detail/load_user_details/".$this->user_id);

        }else{
            $this->form_errors = $this->form_errors."<br>".$this->form->errors;
        }
    }

    public function user_check(){
        return false;
    }
    protected function get_groups_n_fields() {
        $obj = new field_groups_m_user();
        $this->user_id = $this->input->post('user_id');
        $groups = $obj->where("visible","1")->where("group_name","shipping_address")->get_iterated();

        foreach ($groups as $group) {
            $this->form->fieldset($group->group_description);
            $fields = $group->reg_fields_m_user->order_by("order")->get();
            foreach ($fields as $field) {
                switch ($field->rendering_class) {
                    case "text":
                        $this->form->text($field->field_name, $field->field_description, $field->validation_rule, $field->default_value, array('title' => $field->field_note));
                        break;
                    case "textarea":
                        $this->form->textarea($field->field_name, $field->field_description, $field->validation_rule, $field->default_value, array('title' => $field->field_note));
                        break;
                    case "radiogroup":
                        $radios = array();
                        $pieces = explode(";", $field->values);
                        foreach ($pieces as $piece)
                            $radios[] = explode("=", $piece);
                        $this->form->radiogroup($field->field_name, $radios, $field->field_description, $field->default_value, $field->validation_rule, array('title' => $field->field_note));
                        break;
                    case "checkbox":
                        $this->form->br();
                        $this->form->checkbox($field->field_name, $field->values, $field->field_description,$field->default_value=="checked" ? TRUE: FALSE, $field->validation_rule, array('title' => $field->field_note));
                        break;
                    case "checkgroup":
                        $options = array();
                        $pieces = explode(";", $field->values);
                        foreach ($pieces as $piece)
                            $options[] = explode("=", $piece);
                        $this->form->checkgroup($field->field_name, $options, $field->field_description, $field->default_value, $field->validation_rule, array('title' => $field->field_note));
                        break;
                    case "select_one"    :
                        $options = array();
                        $pieces = explode(";", $field->values);
                        foreach ($pieces as $piece){
                            $even_more_pieces = explode("=", $piece);
                            $options[$even_more_pieces[0]] = $even_more_pieces[1];
                        }
                        $this->form->select($field->field_name, $options,  $field->field_description, $field->default_value,$field->validation_rule, array('title' => $field->field_note));
                        break;
                    default:
                        break;
                }
                if(strlen($field->field_error_msg)>0){
                    $this->form->set_error($field->field_name, $field->field_error_msg);
                }
            }
        }
        $this->form->indent(150);
        $this->form->hidden(i_handle_posts::OP_CODE,"add_shipping_address");
        $this->form->hidden('user_id', $this->user_id);
        $this->form->hidden('type', "S");

        $this->form->submit();
        $this->form_data[] = $this->form->get();

    }

}
?>